"use strict";

/**
 * @auther wtfssd
 * Created by wtfssd on 2017/11/2
 */
import * as React from 'react'
import {SCREEN_WIDTH} from "../constant";

const {View, ScrollView, Image, ActionSheetIOS} = require('react-native');
const ImagePicker = require('react-native-image-picker');
const ImageCrop = require('react-native-image-crop-picker').default;


const {Actions} = require("react-native-router-flux");

interface ImagePickerCustomButtonOptions {
    name?: string;
    title?: string;
}

interface StorageOptions {
    skipBackup?: boolean;
    path?: string;
    cameraRoll?: boolean;
    waitUntilSaved?: boolean;
}

interface ImagePickerOptions {
    title?: string;
    cancelButtonTitle?: string;
    takePhotoButtonTitle?: string;
    chooseFromLibraryButtonTitle?: string;
    cameraType?: 'front' | 'back';
    mediaType?: 'photo' | 'video' | 'mixed';
    maxWidth?: number;
    maxHeight?: number;
    quality?: number;
    videoQuality?: 'low' | 'medium' | 'high';
    durationLimit?: number;
    rotation?: number;
    allowsEditing?: boolean;
    noData?: boolean;
    storageOptions?: StorageOptions;
}


interface ImageCropOptions {
    cropping?: boolean;
    width?: number;
    height?: number;
    multiple?: boolean;
    path?: string;
    includeBase64?: boolean;
    includeExif?: boolean;
    cropperTintColor?: string;
    cropperCircleOverlay?: boolean;
    maxFiles?: number;
    waitAnimationEnd?: boolean;
    smartAlbums?: string[];
    useFrontCamera?: boolean;
    compressVideoPreset?: string;
    compressImageMaxWidth?: number;
    compressImageMaxHeight?: number;
    compressImageQuality?: number;
    loadingLabelText?: string;
    mediaType?: string;
    showsSelectedCount?: boolean;
    showCropGuidelines?: boolean;
    hideBottomControls?: boolean;
    enableRotationGesture?: boolean;
}

interface Image {
    path: string;
    size: number;
    data: null | string;
    width: number;
    height: number;
    mime: string;
    exif: null | object;
}

interface MyImagePickerProps {
    pickerOptions?: ImagePickerOptions,
    cropOptions?: ImageCropOptions
}


const _default = {
    options: {
        title: null,
        cancelButtonTitle: '取消',
        takePhotoButtonTitle: '拍摄照片',
        chooseFromLibraryButtonTitle: '选择现有照片',
        customButtons: null,
        cameraType: 'back',
        mediaType: 'photo',
        maxWidth: SCREEN_WIDTH,
        allowsEditing: false,
    },
    cropOptions: {
        mediaType: 'photo',
        width: SCREEN_WIDTH,
        cropperCircleOverlay: false,
        cropping: true
    }
};

export interface Response {
    customButton: string;
    didCancel: boolean;
    error: string;
    data: string;
    uri: string;
    origURL?: string;
    isVertical: boolean;
    width: number;
    height: number;
    fileSize: number;
    type?: string;
    fileName?: string;
    path?: string;
    latitude?: number;
    longitude?: number;
    timestamp?: string;
}


class MyImagePicker {
    options?: MyImagePickerProps;

    constructor() {
        this.options = {
            pickerOptions: {},
            cropOptions: {}
        };
    }

    showImagePicker() {
        return new Promise((resolve, reject) => {
            ImagePicker.showImagePicker({
                ..._default.options, ...this.options.pickerOptions,
                allowsEditing: false
            }, (response: Response) => {
                if (response.error) {
                    reject(new Error(response.error));
                }
                else if (response.didCancel) {
                    reject(new Error('user cancel'));
                }
                else {
                    if (this.options.pickerOptions.allowsEditing) {
                        ImageCrop.openCropper({
                            ..._default.cropOptions, ...this.options.cropOptions,
                            path: response.uri
                        }).then(image => {
                            resolve({...response, ...image})
                        }).catch((e) => {

                        });
                    } else {
                        resolve(response);
                    }

                }
            });
        });
    }

}


export default MyImagePicker;
